using System;
using System.IO;
using System.Linq;
using System.Windows.Input;
using ScriptEase.Shell.Controls;
using ScriptEase.Shell.Properties;



namespace ScriptEase.Shell.Commands
{
    internal class EditWebResourceCommand : ICommand
    {
        private readonly WebResourceStatusView _viewModel;



        public EditWebResourceCommand(WebResourceStatusView viewModel)
        {
            _viewModel = viewModel;
        }



        public void Execute(object parameter)
        {
            MappingModel[] selectedItems = _viewModel.SelectedItems.ToArray();
            if (selectedItems.Length == 0) return;

            string editor = Settings.Default.Editor;
            if (!File.Exists(editor)) editor = "Notepad.exe";

            string args = String.Empty;
            foreach (MappingModel selectedItem in selectedItems)
            {
                args += "\"" + selectedItem.FileInfo.FullName + "\"";
                PublicFunc.RunProcess(editor, args);
            }
        }



        public bool CanExecute(object parameter)
        {
            return (_viewModel.SelectedItems.Any());
        }



        public event EventHandler CanExecuteChanged;
    }
}